﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace FCRE.helper
{
    class ProcessHelper
    {
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("User32.dll", EntryPoint = "SendMessage")]//同步
        private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        const int WM_CLOSE = 0x0010;

        /// <summary>
        /// 指定路径应用程序的进程是否在运行
        /// </summary>
        /// <param name="PName">进程名</param>
        /// <param name="PPath">应用程序路径</param>
        /// <returns></returns>
        public static bool isPRunning(string PName, string PPath)
        {
            Process[] myProcesses = Process.GetProcessesByName(PName);
            bool isrunning = false;
            foreach (Process p in myProcesses)
            {
                try
                {
                    string ppath = p.MainModule.FileName.ToLower();
                    if (ppath == PPath.ToLower()) { isrunning = true; break; }
                }catch(Exception ex) { }
            }
            return isrunning;
        }
        /// <summary>
        /// 指定进程是否在运行
        /// </summary>
        /// <param name="PName"></param>
        /// <returns></returns>
        public static bool isPRunning(string PName)
        {
            Process[] myProcesses = Process.GetProcessesByName(PName);
            bool isrunning = false;
            if (myProcesses.Length > 0) isrunning = true; 
            return isrunning;
        }
        /// <summary>
        /// 关闭所有白名单以外的窗口应用程序和文件夹窗口
        /// </summary>
        /// <param name="whitelist">白名单</param>
        /// <returns></returns>
        public static bool KillWindowApp(string whitelist)
        {
            whitelist = "," + whitelist.ToLower() + ",";
            try
            {
                IntPtr ptrWnd = IntPtr.Zero;
                ptrWnd = FindWindow("CabinetWClass", null);
                while (ptrWnd != IntPtr.Zero)//关闭所有的文件夹窗口
                {
                    SendMessage(ptrWnd, WM_CLOSE, 0, 0);
                    ptrWnd = FindWindow("CabinetWClass", null);
                }
                Process[] myProcesses = Process.GetProcesses();
                foreach (Process proc in myProcesses)//关闭有窗口应用程序
                {
                    ptrWnd = Process.GetProcessById(proc.Id).MainWindowHandle;
                    string winappname = proc.ProcessName.ToLower();
                    if (ptrWnd != IntPtr.Zero && !whitelist.Contains("," + winappname + ","))
                    {
                        try
                        {
                            proc.Kill();//简单粗暴
                            //SendMessage(ptrWnd, WM_CLOSE, 0, 0);//温柔的方法//PostMessage
                            //myProcesses = Process.GetProcesses();
                            LogHelper.WriteLog("关闭：" + winappname);
                        }
                        catch (Exception ex)
                        {
                            //MessageBox.Show(e.Message);
                        }
                    }
                }
                return true;
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(e.Message);
                return false;
            }
        }
        /// <summary>
        /// 结束指定的进程
        /// </summary>
        /// <param name="procname"></param>
        /// <returns></returns>
        public static bool killProc(string procname)
        {
            try
            {
                Process[] myProcess = Process.GetProcessesByName(procname);
                foreach (Process process in myProcess)
                {
                    process.Kill();
                }
                return true;
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(e.Message);
                return false;
            }
        }
        /// <summary>
        /// 结束指定的进程,避免误杀
        /// </summary>
        /// <param name="PName"></param>
        /// <param name="PPath"></param>
        /// <returns></returns>
        public static bool killProc(string PName, string PPath,bool isno=true)
        {
            try
            {
                Process[] myProcesses = Process.GetProcessesByName(PName);
                foreach (Process p in myProcesses)
                {
                    string ppath = p.MainModule.FileName;
                    if (ppath == PPath && isno) { p.Kill(); }//杀指定
                    else if (ppath == PPath && !isno) {  }//指定的不杀，杀其它
                    else p.Kill();
                }
                return true;
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(e.Message);
                return false;
            }
        }
    }
}
